//
//  CustomTextField.swift
//  swiftUI_AI
//
//  Created by ww on 2025/3/22.
//

import SwiftUI

// 自定义输入框
struct CustomTextField: View {
    @Binding var text: String
    let placeholder: LocalizedStringKey
    var isValid: Bool

    var body: some View {
        HStack {
            TextField(placeholder, text: $text)
                .font(.system(size: 16))
                .autocorrectionDisabled(true)
                .textInputAutocapitalization(.never)

            Button(action: {
                text = ""
            }) {
                if text.count > 0 {
                    Image("ic_clear")
                }
            }
            .animation(nil, value: UUID())
            .buttonStyle(PlainButtonStyle())
        }.padding(.vertical, 12)
            .overlay(
                Rectangle()
                    .frame(height: 1)
                    .foregroundColor(isValid ? Color.gray.opacity(0.2) : Color.red.opacity(0.5)),
                alignment: .bottom
            )
    }
}

// 自定义密码输入框
struct CustomSecureField: View {
    @Binding var text: String
    let placeholder: LocalizedStringKey
    @Binding var isSecure: Bool

    var body: some View {
        HStack {
            if isSecure {
                SecureField(placeholder, text: $text)
                    .font(.system(size: 16))
            } else {
                TextField(placeholder, text: $text)
                    .font(.system(size: 16))
            }

            Button(action: {
                isSecure.toggle()
            }) {
                Image(isSecure ? "ic_eye_close" : "ic_eye_open")
                    .foregroundColor(.gray)
            }
        }
        .padding(.vertical, 12)
        .overlay(
            Rectangle()
                .frame(height: 1)
                .foregroundColor(Color.gray.opacity(0.2)),
            alignment: .bottom
        )
    }
}
